package com.example.busquerysystem.activity;

import android.app.FragmentTransaction;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapView;
import com.example.busquerysystem.R;
import com.example.busquerysystem.fragment.LineFragment;
import com.example.busquerysystem.fragment.SiteFragment;
import com.example.busquerysystem.fragment.TransferFragment;



public class MapActivity extends AppCompatActivity implements View.OnClickListener {
    // tab换乘图标控件
    ImageView transferImg;
    // tab换乘标题
    TextView transferTextView;
    // 换乘界面布局

    RelativeLayout transferLayout;
    // tab站点图标控件
    ImageView siteImg;
    // tab站点标题
    TextView siteTextView;
    // 站点界面布局
    RelativeLayout siteLayout;
    // tab线路图标控件
    ImageView lineImg;
    // tab线路标题
    TextView lineTextView;
    // 线路界面布局
    RelativeLayout lineLayout;
    // 展示消息
    private android.app.FragmentManager fragmentManager;
    // 换乘信息
    private TransferFragment transferFragment;
    // 站点信息
    private SiteFragment siteFragmentl;
    // 线路信息
    private LineFragment lineFragment;

    private MapView mMapView;
    private BaiduMap BDmap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SDKInitializer.initialize(getApplicationContext());
        setContentView(R.layout.activity_map);
        // 初始化布局控件
        initView();
        fragmentManager = getFragmentManager();
        // 默认tab页
        setTabSelection(0);

    }

    /**
     * 根据 i 的值来设置默认tab页
     * 0 换乘  1 站点  2 线路
     * @param i
     */
    private void setTabSelection(int i) {
        // 每次选中tab都需要清除上次的选中状态
        clearTabSelection();
        // 先开启fragment事务
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        // 然后隐藏掉所有的Fragment 以免有几个Fragment显示在界面上的BUG
        hideFragment(transaction);
        switch (i) {
            case 0:
                // 点击换乘，需要改变控件文字的颜色
                transferTextView.setTextColor(Color.BLACK);
                if (transferFragment == null) {
                    //如果为空就创建一个并添加到界面
                    transferFragment = new TransferFragment();
                    transaction.add(R.id.content, transferFragment);
                } else {
                    //不为空就显示
                    transaction.show(transferFragment);
                }
                break;
            case 1:
                // 点击站点，需要改变控件文字的颜色
                siteTextView.setTextColor(Color.BLACK);
                if (siteFragmentl == null){
                    //如果为空就创建一个并添加到界面
                    siteFragmentl = new SiteFragment();
                    transaction.add(R.id.content,siteFragmentl);
                }else {
                     //不为空就显示
                    transaction.show(siteFragmentl);
                }
                break;
            default:
                lineTextView.setTextColor(Color.BLACK);
                if (lineFragment == null){
                    lineFragment = new LineFragment();
                    transaction.add(R.id.content,lineFragment);
                }else {
                    transaction.show(lineFragment);
                }
                break;

        }
        transaction.commit();
    }

    /**
     * 隐藏Fragment
     * @param transaction
     */
    private void hideFragment(FragmentTransaction transaction) {
        if (transferFragment != null){
            transaction.hide(transferFragment);
        }
        if (siteFragmentl !=null){
            transaction.hide(siteFragmentl);
        }
        if (lineFragment!=null){
            transaction.hide(lineFragment);
        }
    }

    /**
     * 清除选中状态
     */
    private void clearTabSelection() {
        // 颜色
//        transferImg.setImageResource(R.drawable.transfer);
        transferTextView.setTextColor(Color.GRAY);
        siteTextView.setTextColor(Color.GRAY);
        lineTextView.setTextColor(Color.GRAY);

    }

    private void initView() {

        

        transferLayout = (RelativeLayout) findViewById(R.id.transfer_layout);
        transferTextView = (TextView) findViewById(R.id.transfer_textView);
        transferImg = (ImageView) findViewById(R.id.transfer_img);

        siteLayout = (RelativeLayout) findViewById(R.id.site_layout);
        siteTextView = (TextView) findViewById(R.id.site_textView);
        siteImg = (ImageView) findViewById(R.id.site_img);

        lineLayout = (RelativeLayout) findViewById(R.id.line_layout);
        lineTextView = (TextView) findViewById(R.id.line_textView);
        lineTextView = (TextView) findViewById(R.id.line_textView);

        transferLayout.setOnClickListener(this);
        siteLayout.setOnClickListener(this);
        lineLayout.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.transfer_layout:
                setTabSelection(0);
                break;
            case R.id.site_layout:
                setTabSelection(1);
                break;
            case R.id.line_layout:
                setTabSelection(2);
                break;
            default:
                break;

        }
    }

    class MylocationListener implements BDLocationListener{

        //定位请求回调接口
        private boolean isFirthIn = true;
        @Override
        public void onReceiveLocation(BDLocation bdLocation) {
            //

        }
    }



}
